import { _decorator, Component, Node, ProgressBar, Label } from 'cc';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
const { ccclass, property } = _decorator;

@ccclass('HomeProgress')
export class HomeProgress extends Component {
    @property(ProgressBar)
    pb: ProgressBar = null;

    @property(Label)
    label: Label = null;
    _isFinish = false;
    onLoad() {
        EventManager.on(EventTypes.GameEvents.GameLoadFinish, this.onGameLoadFinish, this);
    }

    cd = 5
    curt = 0;
    onEnable() {
        this.pb.progress = 0;
        this.curt = 0;
        this.label.string = '0';
        this._isFinish = false;
    }

    update(dt: number) {
        if (this._isFinish) return;
        this.curt += dt;
        if (this.curt >= this.cd) {
            this.curt = 0;
        }
        this.pb.progress = this.curt / this.cd;
        this.label.string = (Math.floor(this.pb.progress * 100)).toFixed(0);
    }

    onGameLoadFinish() {
        this.curt = this.cd;
        this._isFinish = true;
        this.pb.progress = 1;
        this.label.string = '100';
    }
}

